//
//  DHPayPwdController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/16.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHPayPwdController.h"
#import "NNValidationView.h"
#import "DHPayPasswordController.h"
@interface DHPayPwdController ()<CZPickerViewDelegate,CZPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *confirmMethodBtn;
@property (weak, nonatomic) IBOutlet UITextField *messageTxtFiled;
@property (weak, nonatomic) IBOutlet UITextField *photoTxtField;
@property (weak, nonatomic) IBOutlet UIButton *confirmImgButton;
@property (weak, nonatomic) IBOutlet UIButton *changeImgButton;
@property(nonatomic ,strong)CZPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIView *pView;

@property(nonatomic ,strong)NSString *photoCode;
@property(nonatomic ,strong)NNValidationView *photoView;
@property(nonatomic ,assign)int timesSer;

@end

@implementation DHPayPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    __weak typeof(self) WeakSelf = self;
    self.photoView = [[NNValidationView alloc] initWithFrame:self.pView.bounds andCharCount:4 andLineCount:4];
    [self.pView addSubview:self.photoView];
    self.photoCode = self.photoView.charString;
    self.photoView.changeValidationCodeBlock = ^{
        WeakSelf.photoCode = WeakSelf.photoView.charString;
        
    };
    
}

- (IBAction)postConfirmCode:(UIButton *)sender
{
    if ([self.confirmMethodBtn.currentTitle isEqualToString:@"点击选择认证方式"]) {
        [SVProgressHUD showInfoWithStatus:@"请选择认证方式"];
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",REMOTEURL,@"index.php?act=member_security&op=send_auth_code"];
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[@"key"] = DHNetworkManager.shareManager.userAccount.key;
    dictM[@"type"] = [self checkEmail:self.confirmMethodBtn.currentTitle] ? @"email" : @"mobile";
    
    [DHNetworkManager.shareManager requestWithMethod:MethodGet
                                           UrlString:url
                                              params:dictM.copy
                                          completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            [SVProgressHUD showInfoWithStatus:@"请检查网络"];
            return ;
        }
        
        [SVProgressHUD showInfoWithStatus:json[@"datas"][@"msg"]];
    }];
}
#pragma mark: - 确定， 进入下一步
- (IBAction)nextButtonClick:(UIButton *)sender
{
   
    if (!self.messageTxtFiled.hash) {[SVProgressHUD showInfoWithStatus:@"请输入安全验证码"];return;}
    
    if (![self.photoTxtField.text isEqualToString:[self.photoCode lowercaseString]])
    {[SVProgressHUD showInfoWithStatus:@"请输入正确图形验证码"];return;}
    
    // 验证验证码
    NSString *url = [NSString stringWithFormat:@"%@%@",REMOTEURL,@"index.php?act=member_security&op=auth_submit"];
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[@"key"] = DHNetworkManager.shareManager.userAccount.key;
    dictM[@"type"] = self.type;
    dictM[@"auth_code"] = self.messageTxtFiled.text;
    
    [DHNetworkManager.shareManager requestWithMethod:MethodPost
                                           UrlString:url
                                              params:dictM.copy
                                          completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            [SVProgressHUD showInfoWithStatus:@"请检查网络"];
            return ;
        }
        if (json[@"datas"][@"error"]) {
             [SVProgressHUD showErrorWithStatus:json[@"datas"][@"error"]];
            return;
        }
        self.timesSer = [NSDate date].timeIntervalSince1970;
        
            // 验证成功跳转页面
        [SVProgressHUD showErrorWithStatus:@"验证成功"];
        
        if ([self.type isEqualToString:@"modify_pwd"])
        {
            [self performSegueWithIdentifier:@"changePwdID" sender:nil];
        }
        if ([self.type isEqualToString:@"modify_email"])
        {
            [self performSegueWithIdentifier:@"changeEmailID" sender:nil];
        }
        if ([self.type isEqualToString:@"modify_mobile"])
        {
            [self performSegueWithIdentifier:@"changMobleID" sender:nil];
        }
        if ([self.type isEqualToString:@"modify_paypwd"])
        {
            
            [self performSegueWithIdentifier:@"DHChangePayPwdID" sender:nil];
         
        }
           
     
    }];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isMemberOfClass:[DHPayPasswordController class]]) {
        [(DHPayPasswordController *)segue.destinationViewController setAuth_modify_paypwd:self.timesSer];
    }
}
#pragma mark: - 选择身份认证方式监听
- (IBAction)confirmBtnClick:(UIButton *)sender
{
    self.pickerView = [[CZPickerView alloc] initWithHeaderTitle:@"选择认证方式"
                                              cancelButtonTitle:@"取消"
                                             confirmButtonTitle:@"确定"];
    [self.pickerView needFooterView];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.pickerView show];
}
#pragma mark: - CZPickerViewDataSource CZPickerViewDelegate
- (NSInteger)numberOfRowsInPickerView:(CZPickerView *)pickerView
{
    return self.confirmList.count;
}

#pragma mark 判断邮箱
- (BOOL)checkEmail:(NSString *)email{
    
    //^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w{2,3}){1,3})$
    
    NSString *regex = @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [emailTest evaluateWithObject:email];
    
}


- (NSAttributedString *)czpickerView:(CZPickerView *)pickerView
               attributedTitleForRow:(NSInteger)row
{
    return [[NSAttributedString alloc] initWithString:self.confirmList[row]];
}
- (void)czpickerView:(CZPickerView *)pickerView
didConfirmWithItemAtRow:(NSInteger)row
{
    [self.confirmMethodBtn setTitle:self.confirmList[row] forState:UIControlStateNormal];
}
@end
